<?php

/*
 * Plugin Name: Tgtai_MU_Admin
 * Plugin URI: -----
 * Description: Various admin functions to support the tgtai.net MU site
 * 
 * Version: 1.0
 * Author: Mark Tilly
 * Author URL: http://www.target-info.com
 * License: GPLv2 or later
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/*
 * Remove un-needed dashboard widgets
 * Place simple widget to Targets page if hidden on menu yet it is created
 * Remove automatic backup menu page if not blog 1 (multisite only)
 * Add blogs.dir to automatic backup filter awb_backup_folders
 * 
 */

function tgtai_mu_remove_dashboard_widgets() {
	// Globalize the metaboxes array, this holds all the widgets for wp-admin
 	global $wp_meta_boxes,  $wp_theme_options;

	// Remove the incomming links widget
	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);	

	// Remove right now
	//unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
	unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
	unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
        
        wp_add_dashboard_widget('link_to_target_info_pages', 'Links to Target Info Pages', 'tgtai_mu_dashboard_widget_function');
}

// Hoook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'tgtai_mu_remove_dashboard_widgets' );

function tgtai_mu_dashboard_widget_function() {
	// Display whatever it is you want to show
    global $wp_theme_options;
    
    echo '<li><a href="http://www.memeposts.com/help" target="_blank" >Target Info Help</a></li>';
    echo '<li><a href="http://www.target-info.com/terms/" target="_blank" >Target Info Terms of Service</a></li>';
    echo '<li><a href="http://www.target-info.com/copyright-policy/" target="_blank" >Target Info Copyright Policy</a></li>';
    //Find and display targets/training page
    $pagobj = get_page_by_path('targets');
    if($pagobj != null && $pagobj->post_status == 'private') {
        if (is_multisite()){
            $url = site_url().'/targets/';
        } else {
            $url = site_url().'/targets/';
        }
        echo '<li><a href="'.$url.'" >Link to Training Page</a></li>';
    } else {
        $pagobj = get_page_by_path('training');
        if($pagobj != null && $pagobj->post_status == 'private') {
            if (is_multisite()){
                $url = site_url().'/training/';
            } else {
                $url = site_url().'/training/';
            }
            echo '<li><a href="'.$url.'" >Link to Training Page</a></li>';
        }
    }

} 

//Add the blogs.dir to the automatic backup folders using filter awb_backup_folders
add_filter('awb_backup_folders', 'tgtai_mu_backup_folders');
function tgtai_mu_backup_folders($backups){

    $value = WP_CONTENT_DIR . '/blogs.dir';
    $backups[] = str_replace(ABSPATH, '', $value);
    return $backups;
}
//Remove the automatic backup menu item if not blog 1 and multisite
//Also remove the Targets menu if not SuperAdmin
add_action('admin_init','tgtai_mu_remove_volcanic',1000);
function tgtai_mu_remove_volcanic() {
    global $blog_id;
    global $menu;
    if (empty($menu)) return;  //No menus to remove from
    if (is_multisite() && $blog_id != 1) {
        remove_menu_page('automatic-wordpress-backup/automatic-wordpress-backup.php');
        remove_action('admin_notices', array('cmAWB','accessKeyWarning'));
    }
    if (!current_user_can('manage_sites')){
        remove_menu_page('edit.php?post_type=target_ai');
    }
}